package com.zheng.sms.rpc.service.impl;

import com.aeye.mbr.upms.dao.model.SmsConfig;
import com.aeye.mbr.sms.SmsContext;
import com.aeye.mbr.sms.SmsPlatform;
import com.aeye.mbr.upms.dao.model.SmsTemplate;
import com.zheng.sms.aliyun.AliyunSmsSenderImpl;
import com.zheng.sms.rpc.api.SmsSender;
import com.zheng.sms.rpc.service.SmsApiService;
import com.zheng.sms.rpc.service.SmsConfigService;
import com.zheng.sms.rpc.service.SmsTemplateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;

@Service
public class SmsApiServiceImpl implements ApplicationContextAware, SmsApiService {


    private static Logger log = LoggerFactory.getLogger(SmsApiServiceImpl.class);


    private static final ThreadLocal<SmsContext> contextHolder = new ThreadLocal<>();


    private AutowireCapableBeanFactory beanFactory;

    @Autowired
    SmsConfigService smsConfigService;

    @Autowired
    SmsTemplateService smsTemplateService;

    /**
     * 缓存
     *
     * @param topOrgId
     * @param templateType
     * @return
     */
    public SmsContext getContext(Integer topOrgId, String templateType) {
        SmsConfig entity = smsConfigService.getSmsConfig(topOrgId);
        if (entity != null) {
            SmsTemplate template = smsTemplateService.getSmsTemplate(entity.getId(), templateType);
            if (template != null) {
                SmsContext context = new SmsContext();
                context.setPlatform(entity.getPlatform());
                context.setTopOrgId(topOrgId);
                context.setAppId(entity.getAppId());
                context.setAppSecret(entity.getAppSecret());
                context.setTemplateType(templateType);
                context.setTemplateId(template.getTemplateId());
                context.setSignName(template.getSignName());
                context.setContent(template.getContent());
                context.setSmsConfigId(entity.getId());
                context.setExpireTime(template.getExpireTime());
                context.setMaxTotalCount(entity.getMaxCount());
                context.setMaxTemplateCount(template.getMaxCount());
                return context;
            }
        }
        return null;
    }

    @Override
    public void send(String mobile, Integer topOrgId, String templateType, LinkedHashMap<String, String> templateParams, String ip) {
        SmsContext context = getContext(topOrgId, templateType);
        if (context != null) {
            context.setIp(ip);
        }
        SmsSender sender = getSmsService(context);
        if (sender != null) {
            sender.sendTemplateSms(mobile, templateParams);
        }

    }


    @Override
    public boolean checkSmsCode(String mobile, Integer topOrgId, String templateType, String code) {
        SmsSender sender = getSmsService(getContext(topOrgId, templateType));
        if (sender != null) {
            return sender.checkSmsCode(mobile, code, templateType);
        }
        return false;
    }


    @Override
    public void clearSmsCode(String mobile, Integer topOrgId, String templateType) {
        SmsSender sender = getSmsService(getContext(topOrgId, templateType));
        if (sender != null) {
            sender.clearSmsCode(mobile, templateType);
        }
    }


    @Override
    public String sendVerifyCode(String mobile, Integer topOrgId, String templateType, int length, String ip) {
        SmsContext context = getContext(topOrgId, templateType);
        if (context != null) {
            context.setIp(ip);
        }
        SmsSender sender = getSmsService(context);
        if (sender != null) {
            return sender.sendVerifyCode(mobile, length);
        }
        return "";
    }

    /**
     * @param topOrgId
     * @return
     */
    public SmsSender getSmsService(Integer topOrgId, String templateType) {
        return getSmsService(getContext(topOrgId, templateType));
    }

    public SmsSender getSmsService(SmsContext context) {
        if (context != null) {
            contextHolder.set(context);
            SmsSender service = null;
            if (SmsPlatform.TYPE_ALIYUN.equals(context.getPlatform())) {
                service = new AliyunSmsSenderImpl();
            }
            if (service != null) {
                beanFactory.autowireBean(service);
            }
            return service;
        }
        return null;
    }


    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        beanFactory = context.getAutowireCapableBeanFactory();
    }


    public static SmsContext getContext() {
        return contextHolder.get();
    }
}
